package double_pointer;

public class IsHappy {
    //快乐数

    //求 n 每一位的平方和
    public int nBitSum(int n) {
        int sum = 0;
        while(n != 0) {
            int tmp = n % 10;  //得到个位
            sum += tmp * tmp;
            n /= 10;
        }
        return sum;
    }
    public boolean isHappy(int n) {
        int slow = n;
        int fast = nBitSum(n); //先执行一次
        //直到相遇为止
        while(slow != fast) {
            //slow走一步，fast走两步
            slow = nBitSum(slow);
            fast = nBitSum(nBitSum(fast));
        }
        return slow == 1;
    }
}
